export type ConditionType = 'time' | 'device' | 'sensor' | 'temperature' | 'humidity' | 'brightness' | 'motion'

export type ComparisonOperator = '=' | '>' | '<' | '>=' | '<='

export interface BaseCondition {
  type: ConditionType
  operator: ComparisonOperator
  value: any
}

export interface TimeCondition extends BaseCondition {
  type: 'time'
  value: string // HH:mm 格式
}

export interface DeviceCondition extends BaseCondition {
  type: 'device'
  value: {
    deviceId: string
    state: 'on' | 'off'
  }
}

export interface SensorCondition extends BaseCondition {
  type: 'temperature' | 'humidity' | 'brightness' | 'motion'
  value: number
  sensorId: string
}

export type Condition = TimeCondition | DeviceCondition | SensorCondition

export type ActionType = 
  | 'switch'      // 开关
  | 'brightness'  // 亮度调节
  | 'temperature' // 温度调节
  | 'color'       // 颜色调节
  | 'curtain'     // 窗帘开合度

export interface BaseAction {
  deviceId: string
  type: ActionType
}

export interface SwitchAction extends BaseAction {
  type: 'switch'
  value: 'on' | 'off'
}

export interface BrightnessAction extends BaseAction {
  type: 'brightness'
  value: number // 0-100
}

export interface TemperatureAction extends BaseAction {
  type: 'temperature'
  value: number // 16-30
}

export interface ColorAction extends BaseAction {
  type: 'color'
  value: string // hex color
}

export interface CurtainAction extends BaseAction {
  type: 'curtain'
  value: number // 0-100
}

export type Action = 
  | SwitchAction 
  | BrightnessAction 
  | TemperatureAction 
  | ColorAction 
  | CurtainAction

export interface SceneGroup {
  id: string
  name: string
  description?: string
  createdAt: string
  updatedAt: string
}

export interface SceneTag {
  id: string
  name: string
  color: string
  createdAt: string
  updatedAt: string
}

export interface Scene {
  id: string
  name: string
  description?: string
  groupId?: string
  group?: SceneGroup
  conditions?: Condition[]
  actions?: Action[]
  devices?: Device[]
  isActive: boolean
  userId: string
  createdAt: Date
  updatedAt: Date
}

export interface SceneHistoryActionLog {
  deviceId: string
  deviceName: string
  actionType: string
  value: any
  status: 'SUCCESS' | 'FAILED'
  message?: string
  startTime: Date
  endTime: Date
  duration: number
  retryCount?: number
}

export interface SceneHistory {
  id: string
  sceneId: string
  status: 'SUCCESS' | 'FAILED' | 'PARTIAL_SUCCESS'
  message: string | null
  startTime: Date
  endTime: Date
  duration: number
  actionLogs: SceneHistoryActionLog[]
  error: any
  retryCount: number
  retryOf?: string
  createdAt: Date
}

// 搜索过滤器类型
export interface SceneFilter {
  search: string
  tags: string[]
  status: 'all' | 'active' | 'inactive'
  dateRange: {
    start?: Date
    end?: Date
  }
}

// 新增场景创建接口
export interface CreateSceneInput {
  name: string
  isActive: boolean
  conditions: Condition[]
  actions: Action[]
  tags?: string[]  // 标签ID数组
  deviceIds?: string[]  // 设备ID数组
}

export interface SceneHistoryCreateInput {
  sceneId: string
  status: 'SUCCESS' | 'FAILED' | 'PARTIAL_SUCCESS'
  message?: string
  startTime: Date
  endTime: Date
  duration: number
  actionLogs: ActionLog[]
  error?: Record<string, any>
  retryCount?: number
  retryOf?: string
}

export interface ActionLog {
  deviceId: string
  deviceName: string
  actionType: string
  value: any
  status: 'SUCCESS' | 'FAILED'
  message?: string
  startTime: Date
  endTime: Date
  duration: number
} 